<?php

//订单
$order =  array (
    'receive_status_color' => '',
    'dec_status_name' => '',
    'id' => 330314,
    'ucode' => 'test',
    'declare_code' => 'jrgdx9',
    'batch' => 'HFFTB24077404test',
    'guid' => '17A87BEF-A6FB-CCD5-0499-4F152296DD69',
    'app_type' => '1',
    'app_time' => '20240516110230',
    'app_status' => '2',
    'order_type' => 'E',
    'order_no' => 'UD027577980TV105607',
    'ebp_code' => '530166050L',
    'ebp_name' => '云南聪亿科技有限公司',
    'ebc_code' => '530166050L',
    'ebc_name' => '云南聪亿科技有限公司',
    'goods_value' => '1.80',
    'freight' => '0',
    'currency' => '142',
    'note' => '',
    'declare_status' => '未申报',
    'receive_status' =>
        array (
            'receive_status' => '未放行',
            'receive_status_color' => '',
        ),
    'entry_id' => NULL,
    'seq_no' => NULL,
    'dec_status' => NULL,
    'create_time' => '2024-05-16 11:02:30',
    'update_time' => NULL,
    'delete_time' => NULL,
    'trade_mode' => '9610',
    'company' =>
        array (
            'id' => 2,
            'declare_code' => 'jrgdx9',
            'declare_name' => '测试2',
            'ucode' => 'test',
            'customs' => 'generalNoSign',
            'ebp_code' => '530166050L',
            'ebp_name' => '云南聪亿科技有限公司',
            'ebc_code' => '530166050L',
            'ebc_name' => '云南聪亿科技有限公司',
            'ebc_telephone' => '19803160314',
            'logistics_code' => '350198104K',
            'logistics_name' => '福建泰航国际物流有限公司',
            'agent_code' => '530166050L',
            'agent_name' => '云南聪亿科技有限公司',
            'owner_code' => '530166050L',
            'owner_name' => '云南聪亿科技有限公司',
            'pay_code' => 'XIANJINZHIFU',
            'pay_name' => '现金支付',
            'iac_code' => '530166050L',
            'iac_name' => '云南聪亿科技有限公司',
            'cop_code' => '530166050L',
            'cop_name' => '云南聪亿科技有限公司',
            'dxp_id' => 'DXPENT0000027335',
            'customs_code' => '0101',
            'port_code' => '0103',
            'operator_code' => '4420913928',
            'ems_no' => '',
            'traf_mode' => '9',
            'wrap_type' => '1',
            'status' => 1,
            'sort' => 0,
            'remark' => '2',
            'create_time' => '2023-10-21 10:21:29',
            'update_time' => '2024-03-26 14:47:19',
        ),
    'orderList' =>
        array (
            0 =>
                array (
                    'id' => 496748,
                    'ucode' => 'test',
                    'declare_code' => 'jrgdx9',
                    'batch' => 'HFFTB24077404test',
                    'order_no' => 'UD027577980TV105607',
                    'trade_mode' => '9610',
                    'gnum' => 1,
                    'item_no' => 'UD027585898TV',
                    'item_name' => '儿童玩具',
                    'item_describe' => '',
                    'bar_code' => '无',
                    'unit' => '011',
                    'currency' => '142',
                    'qty' => '1',
                    'price' => '1.80',
                    'total_price' => '1.80',
                    'note' => '',
                    'create_time' => '2024-05-16 11:02:30',
                    'update_time' => NULL,
                    'delete_time' => NULL,
                ),
        ),
);


$xmlGoods = file_get_contents(public_path('xml/order').'/order_info.xml');

preg_match_all('/{\$.*?}/', $xmlGoods, $matches);

foreach ($order['orderList'] as $k=>$goodsInfo) {

    // 从模版里面提取出来  需要替换的变量数组

    // 获得 变量值数组
    $goodsInfo['gnum'] = $k + 1;
    // 将变量数组 替换成 传入的值
    $params = $matches[0];
    $values = [];
    foreach ($params as $param) {
        $key = str_replace(['{$', '}'], '', $param);
        $values[$param] = $goodsInfo[$key] ?? '';
    }
    // 替换操作
    $content[] = strtr($xmlGoods, $values);
}



$xml = file_get_contents(public_path('xml/order').'/order_header.xml');

$order_list = implode(' ',$content);
$order['order_list'] = $order_list;
$order['cop_code']   = $order['company']['cop_code'];
$order['cop_name']   = $order['company']['cop_name'];
$order['dxp_id']     = $order['company']['dxp_id'];
$order['app_time']   = date("YmdHis");
$order['guid']       = uuid();
// 从模版里面提取出来  需要替换的变量数组
preg_match_all('/{\$.*?}/', $xml, $matches);

// 将变量数组 替换成 传入的值
$params = $matches[0];
$values = [];
foreach ($params as $param) {
    $key = str_replace(['{$', '}'], '', $param);
    $values[$param] = $order[$key] ?? '';
}
// 替换操作
$xml = strtr($xml, $values);

echo $xml;


function uuid()
{
    $chars = md5(9999);
    $value = substr($chars, 0, 8) . '-' . substr($chars, 8, 4) . '-';
    $value .= substr($chars, 12, 4) . '-' . substr($chars, 16, 4) . '-';
    return strtoupper($value . substr($chars, 20, 12));
}
// 获得模版路径
function public_path ($pwd)
{
    return getcwd().'/public/'.$pwd;
}
